//
//  MessageAndTalkAboutViewController.m
//  PYWGetColor
//
//  Created by 杜文杰 on 2018/6/15.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "MessageAndTalkAboutViewController.h"
#import "MessageTopView.h"
#import "MessageAndTalkTableViewCell.h"
#import "WJMessagelistViewController.h"

@interface MessageAndTalkAboutViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *mainTableView; // 主框架
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) NSMutableArray *datamuArray;
@end

@implementation MessageAndTalkAboutViewController
- (void)systemMessageList {
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=system_message" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"error_code"] isEqualToString:@"0"]) {
            self.datamuArray = responseObject[@"data"];
            [self.mainTableView reloadData];
        }else{
            ShowMessage(@"获取数据失败");
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    self.navigationController.navigationBar.hidden = NO;
    // 隐藏导航栏
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
//     self.navigationController.navigationBar.hidden = NO;
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.title = @"消息";
    
    MessageTopView *topView = [[MessageTopView alloc]init];
    CGRect viewRect = CGRectMake(0, 0, SCREEN_WIDTH, 40);
    topView.frame = viewRect;
    
    topView.firstBtn.selected = YES;
    if (topView.firstBtn.isSelected) {
        [topView.firstBtn setImage:[UIImage imageNamed:@"list_sj.2"] forState:UIControlStateNormal];
        [topView.firstBtn setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        self.typeStr = @"SYSM";
        [self systemMessageList];
    }
    
    //环信ID:@"8001"
    //聊天类型:EMConversationTypeChat
//    EaseConversationListViewController *chatListVC = [[EaseConversationListViewController alloc] init];
    WJMessagelistViewController *chatListVC = [[WJMessagelistViewController alloc] init];
    UIView *chatControllerView = chatListVC.view;
    chatControllerView.frame = CGRectMake(0,topView.bottom, SCREEN_WIDTH, 500);
    chatListVC.pushTalkAboutVcBlock = ^(UIViewController *vc) {
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    [self.view addSubview:topView];
    topView.btnTagBlock = ^(NSString *btnstr) {
        if ([btnstr isEqualToString:@"1"]) {
            NSLog(@"系统消息");
            self.typeStr = @"SYSM";
            [self systemMessageList];
            self.mainTableView.hidden = NO;
            chatControllerView.hidden = YES;
        }
        if ([btnstr isEqualToString:@"2"]) {
            NSLog(@"聊天互动");
            self.mainTableView.hidden = YES;
            chatControllerView.hidden = NO;
            
            [self.view addSubview:chatControllerView];
            chatListVC.showRefreshHeader = YES;
            //首次进入加载数据
            [chatListVC tableViewDidTriggerHeaderRefresh];
        }
    };
    
    [self.view addSubview:self.mainTableView];
    __weak typeof(self)weakself = self;
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_bottom);
        make.left.right.bottom.equalTo(weakself.view);
    }];
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datamuArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.typeStr isEqualToString:@"SYSM"]) {
        self.title = @"消息";
        MessageAndTalkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MessageAndTalkTableViewCell class])];
        cell.dataDic = self.datamuArray[indexPath.row];
        return cell;
    }else{
        self.title = @"聊天";
        if (indexPath.section == 0) {
            MessageAndTalkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MessageAndTalkTableViewCell class])];
            return cell;
        }
    }
    
    return nil;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 200;
//}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10 / HEIGHT_6S_SCALE;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = getColor(dividerColor);
    
    return bgView;
}
#pragma mark - init
- (UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaBottomHeight - SafeAreaTopHeight) style:UITableViewStyleGrouped];
        _mainTableView.backgroundColor = getColor(whiteColor);
        _mainTableView.tableHeaderView = self.headerView;
        _mainTableView.tableFooterView = self.footerView;
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.showsHorizontalScrollIndicator = NO;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        // 动态行高
        _mainTableView.rowHeight = UITableViewAutomaticDimension;
        // 预估行高
        _mainTableView.estimatedRowHeight = 80;
        [_mainTableView registerClass:[MessageAndTalkTableViewCell class] forCellReuseIdentifier:NSStringFromClass([MessageAndTalkTableViewCell class])];
    }
    return _mainTableView;
}
- (UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
        _headerView.backgroundColor = [UIColor grayColor];
    }
    return _headerView;
}
- (UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}
- (NSMutableArray *)datamuArray
{
    if (!_datamuArray) {
        _datamuArray = @[].mutableCopy;
    }
    return _datamuArray;
}
@end

